/* 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Title : com.Angaliwa.DaisyChain
Author : Dave Jansen -- Zenitum
URL : http://www.zenitum.com/

Description : This module takes care of all daisyChain 
			  actions for the other Angaliwa modules.

History		: v1.2.090720 Updated the module to 
			  support adding multiple functions to 
			  each daisyChain method. aside from g()
			  all functions are backwards compatible
			  with v1.0. d() deletes all functions
			  from the specified daisyChain action.
			  
			  v1.1.090717 a() now accepts either a 
			  String variable or an array for n. If
			  an array is provided, the provided 
			  function (f) will be added to each of 
			  the daisyChain actions defined in the 
			  array.
			  
			  Added d() function to remove previously
			  defined daisyChain actions.
			  
			  v1.0.090713 Initial version

Created 	: July 13th, 2009

Version		: 1.2
Compatibility: 1.2

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

package com.Angaliwa{
	import flash.display.CapsStyle;
	
	public class DaisyChain{
		private var parentObject:*,
					container:Object = [];
		
		/*
		 * Summary:      	Initializes the DaisyChain module, and sets up
		 					parentObject for future usage.
		 * Return:       	Void
		 * Version:   	 	1.0
		 * Compatibility:	1.0
		 */
		public function DaisyChain(parent:*){
			parentObject = parent;
		}
		
		/*
		 * Summary:      	Adds a daisyChain action.
		 * Return:       	* (parentObject)
		 * Version:   	 	1.1
		 * Compatibility:	1.0
		 */
		public function a(n:*, f:Function):*{
			if(n is String){
				if(!container[n])
					container[n] = new Array();
				
				container[n].push(f);
			}
			else if(typeof n == "object" && n.length>0){
				n.forEach(function(item:*, index:int, array:Array):void{
					a(item, f);
				});
			}
			
			return parentObject;
		}
		
		/*
		 * Summary:      	Gets and returns the selected daisyChain
		 					function, if present.
		 * Return:       	* (parentObject)
		 * Version:   	 	1.0
		 * Compatibility:	1.0
		 */
		public function g(n:String):Function{
			if(typeof container[n] != "undefined")
				return container[n];
			
			return undefined;
		}
		
		/*
		 * Summary:      	Removes the selected daisyChain event(s).
		 * Return:       	* (parentObject)
		 * Version:   	 	1.0
		 * Compatibility:	1.0
		 */
		public function d(n:String):Boolean{
			if(typeof container[n] != "undefined"){
				delete container[n];
				return true;
			}
			
			return false;
		}
		
		
		/*
		 * Summary:      	Runs the specified daisyChain action, if present.
		 * Return:       	* (parentObject)
		 * Version:   	 	1.1
		 * Compatibility:	1.0
		 */
		public function run(n:String,o:Object=undefined):Boolean{
			var g:Boolean=false;
			
			if(container[n] is Array){
				for(var i:int=0; i < container[n].length; i++){
					if(container[n][i] is Function)
						container[n][i](o);
						g = true;
				}
			}
			
			return g;
		}
	}
}